
//
//  XCTextView.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/3/10.
//  Copyright © 2016年 小陈. All rights reserved.
// 发微博的占位文字的设置

import UIKit

class XCPlaceholderTextView: UITextView {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var XCplaceholderTextView: String?{
        didSet{
            placeHolderLable.text = XCplaceholderTextView
            placeHolderLable.font = self.font
            placeHolderLable.sizeToFit()
        }
    }
    
    private lazy var placeHolderLable : UILabel = {
    
    let lable = UILabel()
    lable.text = "分享微博..."
    lable.font = self.font
    lable.textColor = UIColor.lightGrayColor()
    lable.translatesAutoresizingMaskIntoConstraints = false  //添加系统自带的约束是 要先取消 Autoresize
    lable.sizeToFit()
    self.addConstraint(NSLayoutConstraint(item: lable, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 5))
    self.addConstraint(NSLayoutConstraint(item: lable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 8))
   
    self.addSubview(lable)

        return lable
    }()
    
 
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        /// 创建通知监听占位文字是否隐藏
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "TextViewDidChange", name: "UITextViewTextDidChangeNotification", object: self)
        
    }
    
    /// 移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func TextViewDidChange(){
        /// 有文字隐藏
        placeHolderLable.hidden = hasText()
    }
    

   
    
    
}
